<template>
  <div class="showLoading">
    <customDetailsTopbar :title="`${checkedTag}详情页`" flag @back="back" />
    <div class="d-scroll listInner px-1">
      <div class="py-1 px-1 bg-white">
        <div v-for="(item, index) in isExitDescList" :key="index + item.label">
          <custom-details-item :icon="item.icon" :label="item.label" :value="item.value">
            <template #valueSlot>
              <span v-if="item.label !== '评估报告'">{{ item.value }}</span>
              <div v-else>
                <template v-if="item.attachLsit && item.attachLsit.length!==0">
                  <el-link type="primary" @click="openAttachPopUp(item.attachLsit,'attachPopUp')">查看</el-link>
                </template>
                <span v-else>暂无</span>
              </div>
            </template>
          </custom-details-item>
        </div>
      </div>
    </div>
    <attachPopUp ref="attachPopUp" file-name-key="fileName" file-url-key="fileUrl" />
  </div>
</template>

<script>
import customDetailsItem from '@/components/globals/custom-details-item.vue'
import { energyEvaluateById } from '@/api/energySources'
import commonList from '@/assets/mixin/commonList'
import CustomDetailsTopbar from '@/components/common/custom-details-topbar.vue'

export default {
  components: { CustomDetailsTopbar, customDetailsItem },
  mixins: [commonList],
  inject: ['layout'],
  data() {
    return {
      id: '',
      checkedTag: '',
      current: '',
      descList: [
        { label: '企业名称', fieldName: 'companyName', value: '/', icon: '&#xe790;' },
        { label: '年份', fieldName: 'year', value: '/', icon: '&#xe8d5;' },
        { label: '行业类别', fieldName: 'industryTypeName', value: '/', icon: '&#xe634;' },
        { label: '是否重点用能单位', fieldName: 'isEnergyName', value: '/', icon: '&#xe87e;' },
        { label: this.$route.query.tag === '集团诊断评估' ? '评估方' : '评估机构', fieldName: 'estimate', value: '/', icon: '&#xe790;' },
        { label: '评估时间', fieldName: 'energyTime', value: '/', icon: '&#xe685;' },
        { label: '评估报告', fieldName: 'attachName', value: '/', uuid: '', icon: '&#xe677;', attachLsit: [] }
      ],
      descList1: [
        { label: '企业名称', fieldName: 'companyName', value: '/', icon: '&#xe790;' },
        { label: '行业类别', fieldName: 'industryTypeName', value: '/', icon: '&#xe634;' },
        { label: '是否重点用能单位', fieldName: 'isEnergyName', value: '/', icon: '&#xe87e;' },
        { label: '年份', fieldName: 'year', value: '/', icon: '&#xe8d5;' },
        { label: '集团是否评估', fieldName: 'isGroup', value: '/', icon: '&#xe8d5;' },
        { label: '企业是否评估', fieldName: 'isEnterprise', value: '/', icon: '&#xe8d5;' },
        { label: '第三方是否评估', fieldName: 'isParty', value: '/', icon: '&#xe8d5;' }
      ]
    }
  },
  computed: {
    isExitDescList() {
      const { tag } = this.$route.query
      if (tag === '汇总展示') {
        return this.descList1
      } else {
        return this.descList
      }
    }
  },
  mounted() {
    const { id, tag, current, details } = this.$route.query
    this.id = id || ''
    this.checkedTag = tag || ''
    this.current = current || ''
    if (tag === '企业诊断评估' || tag === '第三方诊断评估' || tag === '汇总展示') {
      this.energyEvaluate(JSON.parse(details))
    } else {
      this.energyEvaluateById()
    }
  },
  methods: {
    back() {
      this.$router.push({
        path: '/staging/energySavingDiagnosis',
        query: { current: this.current, checkedTag: this.checkedTag }
      })
    },
    async energyEvaluateById() {
      this.layout.showAreaLoading(['.bg-white'])
      const { status, data } = await energyEvaluateById({ id: this.id })
      if (status === 0 && data) {
        this.setDescList(data)
      }
      this.layout.hideAreaLoading(['.bg-white'])
    },
    // 使用从query中传递的数据
    energyEvaluate(data) {
      if (data) {
        this.setDescList(data)
      }
    },
    // descList数据处理公共方法
    setDescList(data) {
      this.isExitDescList.forEach((v) => {
        v.value = data[v.fieldName] !== '' ? data[v.fieldName].toString() : '/'
        if (v.label === '评估报告') {
          v.uuid = data.attachLsit[0].attachUuid
          v.value = data.attachLsit[0].attachName
          v.attachLsit = data.attachLsit
        }
      })
    }
  }
}
</script>
